package com.ycsj.goldmedalnewconcept.common.util;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;

import com.ycsj.common.manager.PopUpManager;
import com.ycsj.common.view.scroll.MyGridView;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.adapter.TeacherGvAdapter;
import com.ycsj.goldmedalnewconcept.common.bean.TeacherInfo;

import java.util.List;

/**
 * 展示所有老师弹框的PopUpWindow的通用工具类
 * Created by Administrator on 2018/6/28 0028.
 */

public class TeacherPoputil {

    private static Handler handler = new Handler();

    public static void showTeacherPop(final Context context, final List<TeacherInfo.ListBean> teacherinfos, final int selectPos, View llTitle, final onPopItemClickListener listener) {
        if (null == teacherinfos || teacherinfos.size() == 0) {
            //证明没有班级列表数据,不做任何操作
            return;
        } else {
            PopUpManager.showPop(context, R.layout.teacher_pop_class, 1.0f, llTitle, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {
                    //展示班级列表的GridView
                    MyGridView gv = view.findViewById(R.id.gv);

                    //第一步:设置默认的选中状态的班级位置
                    syncClassGvState(teacherinfos, selectPos);

                    //第二步:设置列表数据
                    final TeacherGvAdapter classGvAdapter = new TeacherGvAdapter(context, teacherinfos, R.layout.teacher_gv_class);
                    gv.setAdapter(classGvAdapter);

                    //第三步:设置点击事件
                    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

                            //回调接口
                            if (null != listener) {
                                listener.onItemClick(pos);
                                //需要进行的操作
                                //第一:记录选中的位置
                                //第二:根据当前的位置去请求页面数据
                            }

                            //同步GridView的状态,设置列表的选中状态
                            syncClassGvState(teacherinfos, pos);

                            //通知适配器刷新
                            classGvAdapter.notifyDataSetChanged();

                            //延迟关闭弹框
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    PopUpManager.dismiss();
                                }
                            }, 80);

                        }
                    });
                }
            });
        }
    }


    /**
     * 同步班级列表GridView的选中和没选中的状态
     */
    private static void syncClassGvState(List<TeacherInfo.ListBean> classInfos, int selectPos) {
        for (int i = 0; i < classInfos.size(); i++) {
            if (selectPos == i) {
                classInfos.get(i).isSelected = true;
            } else {
                classInfos.get(i).isSelected = false;
            }
        }
    }

    /**
     * 重置Handler对象为null
     */
    public static void release() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }


    /**
     * 回调接口
     */
    public interface onPopItemClickListener {
        void onItemClick(int pos);
    }
}